package cn.kz.hrm.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SMSRegisterCodeUtil {

//    //用户id
//    private static final String UID = "蔠嚸kangz";
//    //秘钥
//    private static final String SECURITY_KEY = "d41d8cd98f00b204e980";

    /*public static void sendSMSRegisterCode(String phone, String content) {
        HttpClient client = null;
        PostMethod post = null;
        try {
            client = new HttpClient();
            post = new PostMethod("http://utf8.api.smschinese.cn/");
            post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");//在头文件中设置转码
            NameValuePair[] data = {new NameValuePair("Uid", UID)
                    , new NameValuePair("Key", SECURITY_KEY),
                    new NameValuePair("smsMob", phone),//发送的电话号码，用户的电话
                    new NameValuePair("smsText", content)};//发送的正文
            post.setRequestBody(data);

            client.executeMethod(post);
            Header[] headers = post.getResponseHeaders();
            int statusCode = post.getStatusCode();
            System.out.println("statusCode:" + statusCode);
            for (Header h : headers) {
                System.out.println(h.toString());
            }
            String result = new String(post.getResponseBodyAsString().getBytes("utf8"));
            System.out.println(result); //打印返回消息状态
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (post != null) {
                post.releaseConnection();
            }
        }
    }*/

    public static String sendPost(String url, Map<String,String> params){
        try {
            //创建http客户端
            HttpClient client = new HttpClient();
            //创建post请求，指定请求地址
            PostMethod post = new PostMethod(url);
            //设置请求头
            post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码
            //添加参数：循环map，把map中的数据变成List
            Set<String> keys = params.keySet();
            List<NameValuePair> paramList = new ArrayList<>();
            for(String key: keys){
                String value = params.get(key);
                paramList.add(new NameValuePair(key, value));
            }
            //把list变成数组
            NameValuePair[] data = paramList.toArray(new NameValuePair[]{}) ;
            post.setRequestBody(data);
            //执行请求
            client.executeMethod(post);
            //获取结果
            String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
            //打印返回消息状态
            System.out.println(result);
            //释放连接
            post.releaseConnection();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
